package com.zy.cat.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.zy.cat.common.utils.JDAPI;
import com.zy.cat.entity.jd.JDGoods;
import com.zy.cat.entity.jd.JDGoodsInfo;
import com.zy.cat.entity.jd.JDJFGoods;
import com.zy.cat.entity.resp.Pages;
import com.zy.cat.entity.resp.ResultPage;
import com.zy.cat.service.CacheService;
import com.zy.cat.service.JdShopsService;

import jd.union.open.category.goods.get.response.CategoryResp;
import jd.union.open.category.goods.get.response.UnionOpenCategoryGoodsGetResponse;
import jd.union.open.goods.bigfield.query.response.UnionOpenGoodsBigfieldQueryResponse;
import jd.union.open.goods.jingfen.query.response.UnionOpenGoodsJingfenQueryResponse;
import jd.union.open.goods.promotiongoodsinfo.query.response.UnionOpenGoodsPromotiongoodsinfoQueryResponse;
import jd.union.open.goods.query.response.UnionOpenGoodsQueryResponse;
import jd.union.open.promotion.bysubunionid.get.response.PromotionCodeResp;
import jd.union.open.promotion.bysubunionid.get.response.UnionOpenPromotionBysubunionidGetResponse;
import jd.union.open.promotion.common.get.response.UnionOpenPromotionCommonGetResponse;
import lombok.extern.slf4j.Slf4j;
@Service
@Slf4j
public class JdShopsServiceImpl implements JdShopsService {
	
	@Autowired
	CacheService cacheService;

	@Override
	public ResultPage<JDGoods> list(int rtype, Long cid1, Long cid2, Long cid3, int stype, String sort, Pages pages, int isCoupon) {
		String key=isCoupon+"_"+rtype+"_"+cid1+"_"+cid1+"_"+cid2+"_"+cid3+"_"+stype+"_"+sort+"_"+pages.getPageNum();
		UnionOpenGoodsQueryResponse resultTemp=cacheService.getJdCatGoods(0, key, pages.getPageNum(), cid1, cid2, cid3,stype,sort,isCoupon);
		if(resultTemp!=null) {
			List<JDGoods> list=JSON.parseArray(JSON.toJSONString(resultTemp.getData()), JDGoods.class);
			if(list!=null&&!list.isEmpty()) {
				/* 计算佣金 */
				List<JDGoods> resultList=list.stream().map(goods->{
					Double fcode=goods.getCommissionInfo().getCommission()*40;
					goods.setFcode(fcode.longValue());
					return goods;
				}).collect(Collectors.toList());
				int totle=resultTemp.getTotalCount().intValue();
				return new ResultPage<JDGoods>(pages.getPageNum(),pages.getPageSize(),totle,resultList);
			}
		}
		return new ResultPage<JDGoods>(pages.getPageNum(),pages.getPageSize(),0,new ArrayList<JDGoods>());
	}

	@Override
	public ResultPage<JDGoods> search(int rtype, String query, String owner, Pages pages, String sort, int stype, int isCoupon) {
		owner=owner==null?"":owner;
		String key=isCoupon+"_"+rtype+"_"+query+"_"+stype+"_"+sort+"_"+pages.getPageNum()+"_"+owner;
		UnionOpenGoodsQueryResponse resultTemp=cacheService.getJdGoodsSearch(0, key, pages.getPageNum(), query,   sort, stype,isCoupon,owner);
		if(resultTemp!=null) {
			List<JDGoods> list=JSON.parseArray(JSON.toJSONString(resultTemp.getData()), JDGoods.class);
			if(list!=null&&!list.isEmpty()) {
				/* 计算佣金 */
				List<JDGoods> resultList=list.stream().map(goods->{
					Double fcode=goods.getCommissionInfo().getCommission()*40;
					goods.setFcode(fcode.longValue());
					return goods;
				}).collect(Collectors.toList());
				int totle=resultTemp.getTotalCount().intValue();
				return new ResultPage<JDGoods>(pages.getPageNum(),pages.getPageSize(),totle,resultList);
			}
		}
		return new ResultPage<JDGoods>(pages.getPageNum(),pages.getPageSize(),0,new ArrayList<JDGoods>());
	}

	@Override
	public JDGoodsInfo info(int rtype, String sid) {
		JDGoodsInfo result=null;
		UnionOpenGoodsPromotiongoodsinfoQueryResponse  resultTemp=cacheService.getJdGoodsInfo(0, sid);
		if(resultTemp!=null) {
			result=JSON.parseObject(JSON.toJSONString(resultTemp.getData()[0]), JDGoodsInfo.class);
			Double fcode=result.getCommisionRatioWl()*40;
			result.setFcode(fcode.longValue());
		}
		return result;
	}

	@Override
	public PromotionCodeResp convert(int atype,int ctype, int rtype, String pid, String mid, String cid) {
		 UnionOpenPromotionBysubunionidGetResponse resultTemp=JDAPI.getInstance(atype).getGoodsConvertBySbUnionid(pid, mid, cid, ctype);
		 if(resultTemp!=null&&resultTemp.getData()!=null) {
			 return resultTemp.getData();
		 }else {
			 log.error("京东自定义转链失败--》默认无优惠券转链-{}-{}-{}->",pid,mid,cid);
			 UnionOpenPromotionCommonGetResponse result=JDAPI.getInstance(atype).getGoodsConvert(pid,mid, "", ctype);
			 if(result!=null&&result.getData()!=null) {
				 PromotionCodeResp results=new PromotionCodeResp();
				 results.setClickURL(result.getData().getClickURL());
				 return results;
			 }
		 }
		 return null;
	}

	@Override
	public List<JDGoods> like(int rtype, Long cid1, Long cid2, Long cid3) {
		String key=rtype+"_"+cid1+"_"+cid1+"_"+cid2+"_"+cid3;
		List<JDGoods> result=null;
		UnionOpenGoodsQueryResponse resultTemp=cacheService.getJdGoodsLike(key, 0, cid1, cid2, cid3);
		if(resultTemp!=null) {
			result=JSON.parseArray(JSON.toJSONString(resultTemp.getData()), JDGoods.class);
		}
		return result;
	}

	@Override
	public List<CategoryResp> catList(int rtype, int parent, int grade) {
		UnionOpenCategoryGoodsGetResponse resultTemp=cacheService.getJdGoodsCat(parent,grade);
		if(resultTemp!=null) {
			return Arrays.asList(resultTemp.getData());
		}
		return null;
	}

	@Override
	public ResultPage<JDJFGoods> jxList(int rtype, int eid, int stype, String sort, Pages pages) {
		String key=rtype+"_"+eid+"_"+stype+"_"+sort+"_"+pages.getPageNum();
		UnionOpenGoodsJingfenQueryResponse resultTemp=cacheService.getJdJXGoods(key,  eid,   stype,   sort, pages);
		if(resultTemp!=null) {
			List<JDJFGoods> list=JSON.parseArray(JSON.toJSONString(resultTemp.getData()), JDJFGoods.class);
			if(list!=null&&!list.isEmpty()) {
				/* 计算佣金 */
				List<JDJFGoods> resultList=list.stream().map(goods->{
					Double fcode=goods.getCommissionInfo().getCommission()*40;
					goods.setFcode(fcode.longValue());
					log.info("-佣金-->{}",goods.getCommissionInfo().getCommission());
					log.info("-券后佣金-->{}",goods.getCommissionInfo().getCouponCommission());
					
					//计算券后价
					JDJFGoods.PriceInfo priceInfo = goods.getPriceInfo();
		            String lowestPrice = StringUtils.isEmpty(priceInfo.getLowestPrice()) ? priceInfo.getPrice() : priceInfo.getLowestPrice();
		            priceInfo.setLowestPrice(lowestPrice);
					//设置优惠券
		            if (goods.getCouponInfo() != null && goods.getCouponInfo().getCouponList()!=null && goods.getCouponInfo().getCouponList().size()>0) {
		            	JDJFGoods.CouponInfo.Coupon coupon = goods.getCouponInfo().getCouponList().get(0);
		                if (Float.parseFloat(priceInfo.getLowestPrice()) >= Float.parseFloat(coupon.getQuota())) {
		                    //自动计算券后价
		                	BigDecimal b1=new BigDecimal(Float.toString(Float.parseFloat(lowestPrice)));
		                    BigDecimal b2=new BigDecimal(Float.toString(Float.parseFloat(coupon.getDiscount())));
		                	priceInfo.setLowestCouponPrice(b1.subtract(b2).floatValue()+"");
		                } 
		            }
					return goods;
				}).collect(Collectors.toList());
				return new ResultPage<JDJFGoods>(pages.getPageNum(),pages.getPageSize(),resultTemp.getTotalCount().intValue(),resultList);
			}
			return new ResultPage<JDJFGoods>(pages.getPageNum(),pages.getPageSize(),resultTemp.getTotalCount().intValue(),new ArrayList<JDJFGoods>());
		}
		return new ResultPage<JDJFGoods>(pages.getPageNum(),pages.getPageSize(),0,new ArrayList<JDJFGoods>());
	}

	/**
	  * 获取今日推荐商品信息
	 * @param rtype
	 * @param sex
	 * @param pages
	 */
	@Override
	public ResultPage<JDGoods> getTJList(int rtype,int stype, String sort, Pages pages, int isCoupon) {
		String key=isCoupon+"_"+rtype+"_"+stype+"_"+sort+"_"+pages.getPageNum();
		UnionOpenGoodsQueryResponse resultTemp=cacheService.getJdTjGoods(0, key, pages.getPageNum(),stype,sort,isCoupon);
		if(resultTemp!=null) {
			List<JDGoods> list=JSON.parseArray(JSON.toJSONString(resultTemp.getData()), JDGoods.class);
			if(list!=null&&!list.isEmpty()) {
				/* 计算佣金 */
				List<JDGoods> resultList=list.stream().map(goods->{
					Double fcode=goods.getCommissionInfo().getCommission()*40;
					goods.setFcode(fcode.longValue());
					return goods;
				}).collect(Collectors.toList());
				int total=resultTemp.getTotalCount().intValue();
				return new ResultPage<JDGoods>(pages.getPageNum(),pages.getPageSize(),total,resultList);
			}
		}
		return new ResultPage<JDGoods>(pages.getPageNum(),pages.getPageSize(),0,new ArrayList<JDGoods>());
	}
	
//@Override
//public ResultPage<JDGoods> getTJList(Integer rtype, Integer sex, Pages pages) {
//		// TODO Auto-generated method stub
//		String key=rtype+"_"+sex+"_"+pages.getPageNum();
//		ResultPage<JDGoods> resultTemp=cacheService.getTJList(key,sex, pages);
//		if(resultTemp!=null) {
//			List<JDGoods> list=JSON.parseArray(JSON.toJSONString(resultTemp.getList()), JDGoods.class);
//		}
//		return resultTemp;
//}
}